package com.dyg.controller.admin;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dyg.model.Resource;
import com.dyg.model.Role;
import com.dyg.model.RoleResource;
import com.dyg.service.ResourceService;
import com.dyg.service.RoleService;
import com.dyg.service.RoleresourceService;

@Controller
@RequestMapping("/admin/role")
public class RoleController {
	
	@Autowired
	private RoleService roleService;
	
	@Autowired
	private ResourceService resourceService;
	
	@Autowired
	private RoleresourceService roleresourceService;

	
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	public String list(Model model){
		List<Role> findRoleList = roleService.findRoleList();
		model.addAttribute("roleList",findRoleList);
		return "role/list";
	}
	
	@RequestMapping(value = "/update/{id}",method = RequestMethod.GET)
	public String update(@PathVariable("id") Integer id,Model model){
		Role role = roleService.findRoleById(id+"");
		model.addAttribute("role",role);
		return "role/update";
	}
	
	@RequestMapping(value = "/update/{id}",method = RequestMethod.POST)
	public String update(@PathVariable("id") Integer id,Role role){
		role.setId(id);
		roleService.updateRole(role);
		return "redirect:list";
	}
	
	@RequestMapping(value = "/resources/{id}",method = RequestMethod.GET)
	public String listResources(@PathVariable("id") Integer id,Model model){
		/**
		 * 查询角色拥有的资源集合
		 */
		List<Resource> hasResourceList = resourceService.listRoleResource(id);
		
		List<Integer> hasResourceIds = new ArrayList<Integer>();
        for(Resource resource:hasResourceList){
            hasResourceIds.add(resource.getId());
        }
        
        //查询所有资源列表
        List<Resource> resourceAllList = resourceService.findResourceList();
        
        //查询角色
        Role role = roleService.findRoleById(id+"");
        
        model.addAttribute("hasResourceIds",hasResourceIds);
        model.addAttribute("resourceList",resourceAllList);
        model.addAttribute("role",role);
		
		return "role/resources";
	}
	
	
	@RequestMapping(value = "/resource",method = RequestMethod.POST)
	@ResponseBody
	public Map<String,Object> resource(
			Integer roleId,
			Integer resourceId,
			Integer check){
		Map<String,Object> result = new HashMap<String,Object>();
		if(check != null){
			if(check == 0){
				roleresourceService.deleteRoleResource(roleId,resourceId);
			}else if(check == 1){
				RoleResource resource = new RoleResource();
				resource.setRoleId(roleId);
				resource.setResourceId(resourceId);
				roleresourceService.addRoleResource(resource);
			}
			result.put("success", true);
		}else{
			result.put("success",false);
            result.put("errorInfo","数据修改失败");
		}
		return result;
	} 
	
	
	
	
}
